# -*- coding: latin-1 -*-
# Copyright (c) 2007-2009 Jorge Ortiz Romo <jortizromo@gmail.com>
#                         Freymam Vallejo Cuero <freturn@gmail.com>
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:

# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# Import the module and instantiate a pygraph object

def valued_graph_to_matrix (graph):
    """
    Pasa el grafo valorado a una matriz. No distingue si es grafo valorado o con signo.
    """
    
    matrix = []
    nodes = graph.nodes ()
    g = len (nodes)
    
#    print nodes
    
    for i in range (g):
        matrix.append ([])
        for j in range (g):
            try:
                matrix [i].append (graph.edge_weight (nodes [i], nodes [j]))
            except KeyError:
                matrix [i].append (0)
    
    return matrix

def graph_to_matrix (graph):
    """
    Pasa el grafo a una matriz descartando la intensidad en cada relacion, solo 1s y 0s. No distingue si es grafo valorado o con signo.
    """
    
    matrix = []
    nodes = graph.nodes ()
    g = len (nodes)
    
#    print nodes
    
    for i in range (g):
        matrix.append ([])
        for j in range (g):
            has = graph.has_edge (nodes [i], nodes [j])
            if has: matrix [i].append (1)
            else: matrix [i].append (0)
    return matrix

def valued_row_sum (matrix):
    """
    Calcula el vector sum de sumas de valores por filas; sum[i] es la suma de los valores en la fila i.
    """
     
    sum = []
    g = len (matrix)
    
    for i in range (g):
        sum.append (0)
        for j in range (g):
            sum [i] += matrix [i][j]
    
    return sum

def row_sum (matrix):
    """
    Calcula el vector sum de sumas de enlaces por filas; sum[i] es la suma de los enlaces en la fila i.
    """
     
    sum = []
    g = len (matrix)
    
    for i in range (g):
        sum.append (0)
        for j in range (g):
            if matrix [i][j] != 0:
                sum [i] += 1
    
    return sum

def valued_column_sum (matrix):
    """
    Calcula el vector sum de sumas de valores por columnas; sum[j] es la suma de los valores en la columna j.
    """
     
    sum = []
    g = len (matrix)
    
    for i in range (g):
        sum.append (0)
        for j in range (g):
            sum [i] += matrix [j][i]
    
    return sum

def column_sum (matrix):
    """
    Calcula el vector sum de sumas de enlaces por columnas; sum[i] es la suma de los enlaces en la columna i.
    """
     
    sum = []
    g = len (matrix)
    
    for i in range (g):
        sum.append (0)
        for j in range (g):
            if matrix [j][i] != 0:
                sum [i] += 1
    
    return sum
